//
//  XYRobotRockerPointView.m
//  NemoOfficeClient
//
//  Created by 杨旭东 on 2018/3/19.
//  Copyright © 2018年 ainemo. All rights reserved.
//

#import "XYRobotRockerPointView.h"

@interface XYRobotRockerPointView()

@property (nonatomic, assign) CGFloat _radius;

@property (nonatomic, strong) UIImage *normal;

@property (nonatomic, strong) UIImage *highlight;

@property (nonatomic, copy) void (^BreakLimitBlock)();

@property (nonatomic, copy) void (^ResetBlock)();

@property (nonatomic, copy) void (^DirectionBlock)();

@end

@implementation XYRobotRockerPointView

+ (XYRobotRockerPointView *(^)(CGPoint))addXYRobotRockerPoint{
    return ^(CGPoint origin){
        XYRobotRockerPointView *rocker = [[XYRobotRockerPointView alloc] initWithFrame:CGRectMake(origin.x, origin.y, 63, 63)];
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:rocker action:@selector(panAction:)];
        [rocker addGestureRecognizer:pan];
        rocker.userInteractionEnabled = YES;
        return rocker;
    };
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
    }
    return self;
}

- (XYRobotRockerPointView *(^)(CGFloat))xyradius;{
    return ^(CGFloat radius){
        __radius = radius;
        return self;
    };
}

- (XYRobotRockerPointView *(^)(UIImage *, XYRockerPoint))background{
    return ^(UIImage *image, XYRockerPoint type){
        switch (type) {
            case XYRockerPoint_Normal:{
                _normal = image;
                self.image = _normal;
                break;
            }
                
            default:
                _highlight = image;
                break;
        }
        return self;
    };
}

- (void)breakLimit:(void (^)())breakLimit{
    self.BreakLimitBlock = breakLimit;
}

- (void)reset:(void (^)())reset{
    self.ResetBlock = reset;
}

- (void)direction:(void (^)(XYRockerDirection))direction{
    self.DirectionBlock = direction;
}

- (void)panAction:(UIPanGestureRecognizer *)pan{
    CGPoint pointInproc;
    static CGRect frame;
    if (pan.state == UIGestureRecognizerStateBegan) {
        frame = self.frame;
    }
    
    pointInproc = [pan translationInView:self];
    
    if (sqrtf(pointInproc.y * pointInproc.y) <= 43) {
        self.frame = CGRectMake(frame.origin.x , frame.origin.y + pointInproc.y, self.bounds.size.width, self.bounds.size.height);
        [self decideDirection:pointInproc];
    }else{
        CGFloat x, y;
        if (pointInproc.x == 0 && pointInproc.y == 0) {
            x = y = 0;
        }else if (pointInproc.x == 0) {
            x = 0;
            y = 43 * (pointInproc.y > 0 ? 1 : -1);
        }else{
            CGFloat value = pointInproc.y / pointInproc.x;
            x = sqrtf(43 * 43 / (1 + value * value)) * (pointInproc.x > 0 ? 1 : -1);
            y = x * value;
            if (pointInproc.y * y < 0) {
                y = y * -1;
            }
        }
        self.frame = CGRectMake(frame.origin.x , frame.origin.y + y, self.bounds.size.width, self.bounds.size.height);
        [self decideDirection:CGPointMake(x, y)];
    }
    
    if (pan.state == UIGestureRecognizerStateEnded) {
        frame = CGRectZero;
        self.ResetBlock(YES);
        self.image = _normal;
    }
}

- (void)decideDirection:(CGPoint)point{
    if (point.y > 0) {
        self.DirectionBlock(XYRockerDirection_Retreat);
    }else if (point.y < 0){
        self.DirectionBlock(XYRockerDirection_Forward);
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.image = _highlight;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.image = _normal;
    self.ResetBlock(YES);
}

@end
